package com.android.aircondition.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.android.aircondition.R;
import com.android.aircondition.constant.AppConstant;
import com.android.aircondition.service.GreeService;
import com.android.aircondition.service.Windy;

public class WindySpeedActivity extends Activity {
	
	private int[] ress = {R.drawable.windy_light_1,
							R.drawable.windy_light_2,
							R.drawable.windy_light_3,
							R.drawable.windy_light_4,
							R.drawable.windy_light_5};
	
	private int[] imvs = {R.id.windyLight1,
						  R.id.windyLight2,
						  R.id.windyLight3,
						  R.id.windyLight4,
						  R.id.windyLight5};
	
	private GreeService service;
	private Windy windy;
	private int speed;
	
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			if(windy.isAuto()){
				setSpeedLight(speed++);
				speed %= 5;
				sendEmptyMessageDelayed(AppConstant.NO_0, 300);
				
			}
			
		};
	};
	
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.windy);
	}
	
	protected void onResume() {
		super.onResume();
		setViews();
	}
	
	private void setViews() {
		
		service = new GreeService(this);
		windy	= service.getWindyPreference();
		setSpeedLight(windy.getSpeed());
		
		final ImageView windImv = (ImageView)findViewById(R.id.windyFanImageView); 
        final SeekBar   seekBar = (SeekBar)findViewById(R.id.windySeekBar);
        
        seekBar.setProgress(windy.getSpeed()*20);
        
        final Animation windAnimation = new RotateAnimation ( 360, 0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        windAnimation.setDuration ( 5000 - seekBar.getProgress()*45);
        windAnimation.setRepeatCount(Animation.INFINITE);
        windAnimation.setInterpolator(new LinearInterpolator());
        windImv. setAnimation (windAnimation);
        windAnimation. startNow ();
        
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	public void onStopTrackingTouch(SeekBar seekBar)  {
        		service.setWindyPreference(windy);
        	}
        	public void onStartTrackingTouch(SeekBar seekBar) {}
        	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        		
        		int index = progress/20;
        		setSpeedLight(index);
        		windy.setSpeed(index);
        		if(progress == 100){
        			progress = 99;
 				}
 					long dur = 5000 - progress*35;
 					windAnimation.setDuration(dur);
 					
 				}
        });
        
        Button autoBtn = (Button)findViewById(R.id.windyAutoBtn);
        
		if(windy.isAuto()){
			autoBtn.setBackgroundResource(R.drawable.tab_nor);
			mHandler.sendEmptyMessage(AppConstant.NO_0);
			
			seekBar.setEnabled(false);
		}

        autoBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				if(windy.isAuto()){ 
					
					v.setBackgroundResource(R.drawable.tab_selected);
					windy.setAuto(false);
					
		
					seekBar.setEnabled(true);
					
				}else{
					
					v.setBackgroundResource(R.drawable.tab_nor);
					windy.setAuto(true);
					mHandler.sendEmptyMessage(AppConstant.NO_0);
					
					seekBar.setClickable(false);
					seekBar.setPressed(false);
					seekBar.setFocusable(false);
					seekBar.setEnabled(false);
				}
				
				service.setWindyPreference(windy);	
			
			}
		});
           
	}
	
	private void setSpeedLight(int index){
		
		for(int i = 0 ; i < imvs.length ; i++){
			
			if(i<(index+1))
				findViewById(imvs[i]).setBackgroundResource(ress[i]);	
			else
				findViewById(imvs[i]).setBackgroundResource(R.drawable.light_bg);
			
		}
	}
}
